package com.wsg.lightaccounting.Activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.LinearLayout;

import com.wsg.lightaccounting.Fragment.ChartFragment;
import com.wsg.lightaccounting.Fragment.CircularFragment;
import com.wsg.lightaccounting.Fragment.LineFragment;
import com.wsg.lightaccounting.Fragment.MoreFragment;
import com.wsg.lightaccounting.Fragment.RecordFragment;
import com.wsg.lightaccounting.Fragment.WalletFragment;
import com.wsg.lightaccounting.R;

/**
 * Created by dell-pc on 2016/11/19.
 */

public  class MainActivity extends FragmentActivity implements View.OnClickListener,RecordFragment.OnFragmentInteractionListener,WalletFragment.OnFragmentInteractionListener
        ,ChartFragment.OnFragmentInteractionListener,MoreFragment.OnFragmentInteractionListener
        , LineFragment.OnFragmentInteractionListener,CircularFragment.OnFragmentInteractionListener{


    public static final int RECORDFRAGMENT = 1;
    public static final int CHARTFRAGMENT = 2;
    public static final int WALLETFRAGMENT = 3;
    public static final int MOREFRAGMENT = 4;

    private RecordFragment recordFragment;
    private ChartFragment chartFragment;
    private WalletFragment walletFragment;
    private MoreFragment moreFragment;
    private FragmentManager fm;
    private LinearLayout record,chart,plus,wallet,more;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fm = this.getSupportFragmentManager();
        if (savedInstanceState != null) {
            recordFragment = (RecordFragment) fm.findFragmentByTag("record_fragment");
            chartFragment = (ChartFragment) fm.findFragmentByTag("chart_fragment");
            walletFragment = (WalletFragment) fm.findFragmentByTag("wallet_fragment");
            moreFragment = (MoreFragment) fm.findFragmentByTag("more_fragment");
        }
        initView();
    }
    private void initView(){

        record = (LinearLayout) findViewById(R.id.record);
        chart = (LinearLayout) findViewById(R.id.chart);
        plus = (LinearLayout) findViewById(R.id.plus);
        wallet = (LinearLayout) findViewById(R.id.wallet);
        more = (LinearLayout) findViewById(R.id.more);

        record.setOnClickListener(this);
        chart.setOnClickListener(this);
        plus.setOnClickListener(this);
        wallet.setOnClickListener(this);
        more.setOnClickListener(this);

        //resetui();
        //plus.setBackgroundResource(R.mipmap.btn_cancle_normal);

        switchFragment(RECORDFRAGMENT);
        //setBackGroundColor(RECORDFRAGMENT);

    }
    private void switchFragment(int fragment){
        FragmentTransaction transaction = fm.beginTransaction();
        String tag = "";
        hideFragment(transaction);
        switch (fragment){
            case RECORDFRAGMENT:
                if (recordFragment == null) {
                    recordFragment = new RecordFragment();
                    tag = "record_fragment";
                    transaction.add(R.id.fragment,recordFragment,tag);
                }
                else {
                    transaction.show(recordFragment);
                }
                break;
            case CHARTFRAGMENT:
                if (chartFragment == null) {
                    chartFragment = new ChartFragment();
                    tag = "chart_fragment";
                    transaction.add(R.id.fragment,chartFragment,tag);
                }
                else {
                    transaction.show(chartFragment);
                }
                break;
            case WALLETFRAGMENT:
                if (walletFragment == null) {
                    walletFragment = new WalletFragment();
                    tag = "wallet_fragment";
                    transaction.add(R.id.fragment,walletFragment,tag);
                }
                else {
                    transaction.show(walletFragment);
                }
                break;
            case MOREFRAGMENT:
                if (moreFragment == null) {
                    moreFragment = new MoreFragment();
                    tag = "more_fragment";
                    transaction.add(R.id.fragment,moreFragment,tag);
                }
                else {
                    transaction.show(moreFragment);
                }
                break;
            default:
                break;
        }
        transaction.commitAllowingStateLoss();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (recordFragment != null) {
            transaction.hide(recordFragment);
        }

        if (chartFragment != null) {
            transaction.hide(chartFragment);
        }

        if (walletFragment != null){
            transaction.hide(walletFragment);
        }

        if (moreFragment != null){
            transaction.hide(moreFragment);
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.record:
                //setBackGroundColor(RECORDFRAGMENT);
                switchFragment(RECORDFRAGMENT);
                updateAllUI(this);
                break;
            case R.id.chart:
                switchFragment(CHARTFRAGMENT);
                updateAllUI(this);
                break;
            case R.id.plus:
                Intent intent = new Intent();
                intent.setClass(this,AccountActivity.class);
                startActivity(intent);
                break;
            case R.id.wallet:
                //setBackGroundColor(WALLETFRAGMENT);
               switchFragment(WALLETFRAGMENT);
                updateAllUI(this);
                break;
            case R.id.more:
                //setBackGroundColor(MOREFRAGMENT);
                switchFragment(MOREFRAGMENT);
                updateAllUI(this);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
    private void setBackGroundColor(int i) {
        resetui();
        switch (i){
            case RECORDFRAGMENT:
                record.setBackgroundResource(R.mipmap.btn_normal);
                break;
            case CHARTFRAGMENT:
                chart.setBackgroundResource(R.mipmap.btn_normal);
                break;
            case WALLETFRAGMENT:
                wallet.setBackgroundResource(R.mipmap.btn_normal);
                break;
            case MOREFRAGMENT:
                more.setBackgroundResource(R.mipmap.btn_normal);
                break;
            default:
                break;
        }
    }
    private void resetui(){
        record.setBackgroundResource(R.mipmap.detail);
        chart.setBackgroundResource(R.mipmap.diagram);
        wallet.setBackgroundResource(R.mipmap.wallet);
        more.setBackgroundResource(R.mipmap.more);
    }
    public static void updateAllUI(Context context){
        Intent intent1 = new Intent();
        Intent intent2 = new Intent();
        Intent intent3 = new Intent();
        Intent intent4 = new Intent();
        intent1.setAction(LineFragment.UPDATEUI_LINE_FRAGMENT);
        intent2.setAction(CircularFragment.UPDATEUI_CIRCULAR_FRAGMENT);
        intent3.setAction(WalletFragment.UPDATE_WALLET_FRAGMENT);
        intent4.setAction(RecordFragment.UPDATEUI_RECORD_FRAGMENT);
        context.sendBroadcast(intent1);
        context.sendBroadcast(intent2);
        context.sendBroadcast(intent3);
        context.sendBroadcast(intent4);
    }
}
